unit clsTurno;

interface

uses dm,dbtables,biblio,sysutils,classes;

type
  TTurno=Class(TObject)
    public
      Cod_Maquina: string;
      Codigo_Ope : string;
      Descricao  : string;
      Inicio     : string;
      Termino    : string;
      function  Recuperar(Cod_Turno: string): boolean;
      function  Retorna_Ultimo              : string;
      procedure Gravar(Cod_Turno: String);
      procedure Excluir(Cod_Turno: String);
      procedure Consultar(p_Descricao: string; var tq: TQuery);
      procedure Excluir_Todos(p_Cod_Maquina,p_Codigo_Ope: String);
      procedure Listar_Turnos(p_Cod_Maquina,p_Codigo_Ope: string; var l_turnos: TStringList);
    end;

implementation

procedure TTurno.Gravar;
var tq: TQuery;
begin
 tq             := Tquery.Create(DmInt);
 tq.databasename:= dmint.Db.DatabaseName;
 tq.active      := False;
 tq.sql.clear;
 tq.params.clear;
 tq.sql.add('Select * from Turno where Cod_Turno=:a');
 tq.params[0].asstring:= Cod_Turno;
 tq.open;
 if tq.eof then begin
  tq.close;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Insert into Turno values ('+
             ':k_Cod_Turno  ,'+
             ':k_Cod_Maquina,'+
             ':k_Codigo_Ope ,'+
             ':k_Descricao   ,'+
             ':k_Inicio     ,'+
             ':k_Termino    )');
 end else begin
  tq.close;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Update Turno set           '+
             'Cod_Maquina=:k_Cod_Maquina,'+
             'Codigo_Ope =:k_Codigo_Ope ,'+
             'Descricao  =:k_Descricao  ,'+
             'Inicio     =:k_Inicio     ,'+
             'Termino    =:k_Termino     '+
             'where                      '+
             'Cod_Turno  =:k_Cod_Turno   ');
 end;
 tq.parambyname('k_Cod_Turno').asstring  := Cod_Turno;
 tq.parambyname('k_Cod_Maquina').asstring:= Cod_Maquina;
 tq.parambyname('k_Codigo_Ope').asstring := Codigo_Ope;
 tq.parambyname('k_Descricao').asstring  := Descricao;
 tq.parambyname('k_Inicio').asstring     := Inicio;
 tq.parambyname('k_Termino').asstring    := Termino;
 tq.execsql;
 tq.close;
 tq.Free;
end;

procedure TTurno.Excluir;
var tq: TQuery;
begin
 tq             := Tquery.Create(dmint);
 tq.databasename:= dmint.db.databasename;
 tq.active      := False;
 tq.sql.Clear;
 tq.params.clear;
 tq.sql.add('Delete from Turno where Cod_Turno=:a');
 tq.params[0].asstring:= Cod_Turno;
 tq.execsql;
 tq.close;
 tq.Free;
end;

function TTurno.Recuperar;
var tq: TQuery;
begin
 tq             := Tquery.Create(dmint);
 tq.databasename:= dmint.db.databasename;
 tq.active      := False;
 tq.sql.Clear;
 tq.params.clear;
 tq.sql.add('Select * from Turno where Cod_Turno=:a');
 tq.params[0].asstring:= Cod_Turno;
 tq.open;
 if tq.eof then result:= false else result:= true;
 Cod_Maquina:= tq.fieldbyname('Cod_Maquina').asstring;
 Codigo_Ope := tq.fieldbyname('Codigo_Ope').asstring;
 Descricao  := tq.fieldbyname('Descricao').asstring;
 Inicio     := tq.fieldbyname('Inicio').asstring;
 Termino    := tq.fieldbyname('Termino').asstring;
 tq.close;
 tq.free;
end;

function TTurno.Retorna_Ultimo;
var tq: TQuery;
begin
 tq             := Tquery.Create(dmint);
 tq.databasename:= dmint.db.databasename;
 tq.active      := False;
 tq.sql.Clear;
 tq.params.clear;
 tq.sql.add('Select max(Cod_Turno) ultimo from Turno');
 tq.open;
 if tq.fieldbyname('ultimo').asstring='' then
  result:= '0001'
 else
  result:= strzero(tq.fieldbyname('ultimo').asinteger+1,4);
 tq.close;
 tq.Free;
end;

procedure TTurno.Consultar;
begin
 tq.close;
 tq.sql.Clear;
 tq.params.clear;
 tq.sql.add('Select * from Turno where Descricao>=:a order by Descricao');
 if p_Descricao='' then p_Descricao:= ' ';
 tq.params[0].asstring:= p_Descricao;
 tq.open;
end;

procedure TTurno.Listar_Turnos;
var tq: TQuery;
begin
 tq             := Tquery.Create(dmint);
 tq.databasename:= dmint.db.databasename;
 tq.active      := False;
 tq.sql.Clear;
 tq.params.clear;
 tq.sql.add('Select * from Turno where Cod_Maquina=:a and Codigo_Ope=:b');
 tq.params[0].asstring:= p_Cod_Maquina;
 tq.params[1].asstring:= p_Codigo_Ope;
 tq.open;
 l_turnos.clear;
 while not tq.eof do begin
  l_turnos.add(tq.fieldbyname('inicio').asstring+' - '+tq.fieldbyname('termino').asstring);
  tq.next;
 end;
 tq.close;
 tq.Free;
end;

procedure TTurno.Excluir_Todos;
var tq: TQuery;
begin
 tq             := Tquery.Create(dmint);
 tq.databasename:= dmint.db.databasename;
 tq.active      := False;
 tq.sql.Clear;
 tq.params.clear;
 tq.sql.add('Delete from Turno where Cod_Maquina=:a and Codigo_Ope=:b');
 tq.params[0].asstring:= p_Cod_Maquina;
 tq.params[1].asstring:= p_Codigo_Ope;
 tq.execsql;
 tq.close;
 tq.Free;
end;

end.
